package com.ssh.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Console;
import com.ssh.entity.User;
import com.ssh.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import java.awt.*;

/**
 * 用户 控制层
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-21 14:58
 */
@Controller
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private UserService service;

    @RequestMapping("/registered")
    public String addOneMethod(User user, Model mode) {
        return "index";
    }

    /**
     * 登陆方法
     *
     * @param user 用户
     * @param mode 视图解析器
     * @return 转发到主页
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    public String loginMethod(User user, Model mode, String createVerificationCode, HttpServletRequest request) {
        // 放消息
        String message;

        ICaptcha iCaptcha = (ICaptcha) request.getSession().getAttribute("VerificationCode");
        if (createVerificationCode == null || createVerificationCode.length() == 0 || !iCaptcha.verify(createVerificationCode)) {
            // 验证码为空或者和 request 中的不一致，转发到当前页面
            message = "验证码错误！！！";
            request.setAttribute("message", message);
            return "jsp/user/login";
        }
        final User load = service.load(user.getId(), user.getPassword());
        if (load == null) {
            // 登陆失败
            message = "用户名或者密码错误！！！";
            request.setAttribute("message", message);
            return "jsp/user/login";
        }
        request.getSession().setAttribute("user", load);
        return "jsp/user/main";
    }

    /**
     * 创建一个图片验证码，放入 request 中
     *
     * @param request  request
     * @param response response
     */
    @SneakyThrows
    @RequestMapping(value = "/createVerificationCode", method = {RequestMethod.POST, RequestMethod.GET})
    public void createVerificationCode(HttpServletRequest request, HttpServletResponse response) {

        // 设置响应图片
        response.setContentType("image/png");
        // 获 response  的输出流
        final ServletOutputStream outputStream = response.getOutputStream();
        // 创建一个验证码
        final CircleCaptcha lineCaptcha = CaptchaUtil.createCircleCaptcha(200, 100);


        // 放入 Session 域中
        request.getSession().setAttribute("VerificationCode", lineCaptcha);

        // 写出
        lineCaptcha.write(outputStream);
        // 刷新
        outputStream.flush();
        // 关流
        outputStream.close();
    }
}
